package com.li.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.li.utils.TransformUtil;
import com.ndboo.reslibrary.R;

/**
 * Author: Li
 * Date: 2017/9/20 0020
 * Description:
 */

public class DoubleTextView extends LinearLayout {
    public DoubleTextView(Context context) {
        super(context);
    }

    public DoubleTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    private TextView mTxtLeft;
    private TextView mTxtRight;
    private ImageView mImgArrow;

    public ImageView getImgArrow() {
        return mImgArrow;
    }

    private void init(Context context, AttributeSet attrs) {
        View view= LayoutInflater.from(context).inflate(R.layout.layout_double_txt,this);

        mTxtLeft=view.findViewById(R.id.txt_left);
        mTxtRight=view.findViewById(R.id.txt_right);
        mImgArrow=view.findViewById(R.id.img_arrow);

        TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.DoubleTextView);
        String txtLeft=array.getString(R.styleable.DoubleTextView_dtv_txt_left);
        String txtRight=array.getString(R.styleable.DoubleTextView_dtv_txt_right);
        int id=array.getResourceId(R.styleable.DoubleTextView_dtv_txt_left_drawable_left,0);
        int rightId=array.getResourceId(R.styleable.DoubleTextView_dtv_txt_right_drawable_right,0);
        int leftTxtColor=array.getColor(R.styleable.DoubleTextView_dtv_txt_left_color, Color.parseColor("#333333"));
        int rightTxtColor=array.getColor(R.styleable.DoubleTextView_dtv_txt_right_color,Color.parseColor("#333333"));

        int leftTxtSize=array.getDimensionPixelSize(R.styleable.DoubleTextView_dtv_txt_left_size, TransformUtil.sp2px(context,14));
        int rightTxtSize=array.getDimensionPixelSize(R.styleable.DoubleTextView_dtv_txt_right_size, TransformUtil.sp2px(context,14));
        int imgVisible=array.getInt(R.styleable.DoubleTextView_dtv_arrow_visible,GONE);

        array.recycle();

        setTxtLeft(txtLeft);
        setTxtRight(txtRight);
        setLeftTxtDrawableLeft(id);
        setRightTxtDrawableRight(rightId);
        setLeftTextColor(leftTxtColor);
        setRightTextColor(rightTxtColor);
        setLeftTextSize(leftTxtSize);
        setRightTextSize(rightTxtSize);
        mImgArrow.setVisibility(imgVisible);
    }

    public void setTxtLeft(String txt){
        mTxtLeft.setText(txt);
    }
    public void setTxtRight(String txt){
        mTxtRight.setText(txt);
    }
    public void setLeftTxtDrawableLeft(int resourceId){
        mTxtLeft.setCompoundDrawablesWithIntrinsicBounds(resourceId,0,0,0);
    }

    public void setRightTxtDrawableRight(int resourceId){
        mTxtRight.setCompoundDrawablesWithIntrinsicBounds(0,0,resourceId,0);
    }

    public void setLeftTextColor(int color){
        mTxtLeft.setTextColor(color);
    }
    public void setRightTextColor(int color){
        mTxtRight.setTextColor(color);
    }

    public void setLeftTextSize(int px){
        mTxtLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX,px);
    }
    public void setRightTextSize(int px){
        mTxtRight.setTextSize(TypedValue.COMPLEX_UNIT_PX,px);
    }

    public String getTxtRight(){
        return mTxtRight.getText().toString();
    }
}
